﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Collections.ObjectModel;
using bdnt_pets_system.DataServiceReference;
using System.Globalization;
using bdnt_pets_system.Tools;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.FailuresTraking
{
    public partial class AddToTrakingWindow : BaseWindow
    {
        DataServiceReference.DataServiceClient dataClient;

        public AddToTrakingWindow()
        {
            InitializeComponent();
            this.Title = MyStrings.xzzztd;
            dataClient = new DataServiceReference.DataServiceClient();
            InitEvent();
            dataClient.GetTeamMemembersAsync();

        }
        void InitEvent()
        {
            dataClient.GetTeamMemembersCompleted += new EventHandler<DataServiceReference.GetTeamMemembersCompletedEventArgs>(dataClient_GetTeamMemembersCompleted);
            cbZzTd.SelectionChanged += new SelectionChangedEventHandler(cbZzTd_SelectionChanged);
        }
        /// <summary>
        /// 追踪团队编号
        /// </summary>
        public int zzTdId;
        public string zzTdName;
        void cbZzTd_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            wpZzry.Children.Clear();
            if (cbZzTd.SelectedValue == null)
                return;
            if (memeberList == null)
                return;
            zzTdId = Int32.Parse(cbZzTd.SelectedValue.ToString());
            zzTdName = (cbZzTd.SelectedItem as SimpleObject<int, string>).Item2;
            var cuMembers = memeberList.Where(p => p.TeamId == zzTdId).OrderByDescending(p => p.IsManager);
            foreach (var item in cuMembers)
            {
                CheckBox ck = new CheckBox()
                {
                    Margin = new Thickness(0, 0, 10, 5),
                    Tag = item
                };
                TextBlock tb = new TextBlock() { Text = item.EmpName };
                if (item.IsManager)
                {

                    Run r = new Run() { Foreground = new SolidColorBrush(Colors.Red), Text = MyStrings.fzr };
                    tb.Inlines.Add("[");
                    tb.Inlines.Add(r);
                    tb.Inlines.Add("]");

                    //ck.Content = item.EmpName + "[" + r + "]";
                    //ck.Content = item.EmpName + "[负责人]";

                }
                ck.Content = tb;
                wpZzry.Children.Add(ck);
            }
        }
        void InitData()
        {
            InitEvent();
            dataClient.GetTeamMemembersAsync();
        }

        ObservableCollection<Tbl_TeamMemembers> memeberList;
        void dataClient_GetTeamMemembersCompleted(object sender, DataServiceReference.GetTeamMemembersCompletedEventArgs e)
        {
            memeberList = e.Result;
            var tems = HelpTools.DistinctList(memeberList.Select(p => new SimpleObject<int, string>() { Item1 = p.TeamId, Item2 = p.TeamName }).OrderBy(p => p.Item1).ToList());
            cbZzTd.ItemsSource = tems;
            cbZzTd.DisplayMemberPath = "Item2";
            cbZzTd.SelectedValuePath = "Item1";
            cbZzTd.SelectedIndex = tems.Count() > 0 ? 0 : -1;
        }

        /// <summary>
        /// 追踪团队人员编号
        /// </summary>
        public List<int> selectMemembers = new List<int>();
        public string selectMembersName = "";

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in wpZzry.Children)
            {
                var cuCk = (item as CheckBox);
                if (cuCk.IsChecked.Value)
                {
                    var cuv = cuCk.Tag as Tbl_TeamMemembers;
                    selectMemembers.Add(cuv.EmpId);
                    selectMembersName += string.IsNullOrEmpty(selectMembersName) ? cuv.EmpName : "," + cuv.EmpName;
                }
            }
            if (selectMemembers.Count < 1)
            {
                errorMsg.Text = MyStrings.qxzzrr;// "请选择追踪人员";
                return;
            }

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

